package com.Hwadee.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.Hwadee.eduservice.entity.EduCourse;
import com.Hwadee.eduservice.entity.EduCourseDescription;
import com.Hwadee.eduservice.entity.frontvo.CourseFrontVo;
import com.Hwadee.eduservice.entity.frontvo.CourseWebVo;
import com.Hwadee.eduservice.entity.vo.CourseInfoVo;
import com.Hwadee.eduservice.entity.vo.CoursePublishVo;
import com.Hwadee.eduservice.mapper.EduCourseMapper;
import com.Hwadee.eduservice.service.EduChapterService;
import com.Hwadee.eduservice.service.EduCourseDescriptionService;
import com.Hwadee.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.Hwadee.eduservice.service.EduVideoService;
import com.Hwadee.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-29
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //注入课程描述service
    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    //添加课程信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        //插入数据到课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);//CourseInfoVo转换成EduCourse
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }
        //添加成功后，操作
        //获取课程id
        String cid = eduCourse.getId();
        //插入数据到课程描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        descriptionService.save(courseDescription);

        return cid;
    }

    //根据课程id查询课程信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1.查课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //封装成vo
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        //2.查课程描述表
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);//转换成EduCourse类
        int update = baseMapper.updateById(eduCourse);
        if (update <= 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }

        //修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(eduCourse.getId());
        description.setDescription(courseInfoVo.getDescription());
        descriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //最终发布页面数据
        CoursePublishVo coursePublishVo = baseMapper.publishCourseInfo(id);
        return coursePublishVo;
    }

    //根据id删除课程，级联删除，需要先删除 小节表——》章节表 ——》课程描述表 ——》课程表
    @Override
    public void removeCourse(String courseId) {
        //1.根据课程id删除小节
        videoService.removeByCourseId(courseId);
        //2.根据课程id删除章节
        chapterService.removeByCourseId(courseId);
        //3.根据课程id删除课程描述
        descriptionService.removeById(courseId);
        //4.根据课程id删除课程
        int delete = baseMapper.deleteById(courseId);
        if (delete <= 0) {
            throw new GuliException(20001, "删除课程失败！");
        }

    }

    @Cacheable(value = "course", key = "'getIndexCourse'")//注意加单引号！！！
    @Override
    //查询8条热门课程
    public List<EduCourse> getIndexCourse() {
        QueryWrapper<EduCourse> courseWrapper = new QueryWrapper<>();
        courseWrapper.orderByDesc("view_count")
                .last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(courseWrapper);
        return courseList;
    }

    //条件查询带分页查询课程
    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {

        String oneSubject = courseFrontVo.getSubjectParentId();
        String twoSubject = courseFrontVo.getSubjectId();
        String buyCountSort = courseFrontVo.getBuyCountSort();
        String priceSort = courseFrontVo.getPriceSort();
        String gmtCreateSort = courseFrontVo.getGmtCreateSort();

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //判断条件值
        //一级标题
        if (!StringUtils.isEmpty(oneSubject)) {
            wrapper.eq("subject_parent_id", oneSubject);
        }
        //二级标题
        if (!StringUtils.isEmpty(twoSubject)) {
            wrapper.eq("subject_id", twoSubject);
        }
        //buyCountSort
        if (!StringUtils.isEmpty(buyCountSort)) {
            wrapper.orderByDesc("buy_count");
        }

        //priceSort
        if (!StringUtils.isEmpty(priceSort)) {
            wrapper.orderByDesc("price");
        }
        //一级标题
        if (!StringUtils.isEmpty(gmtCreateSort)) {
            wrapper.orderByDesc("gmt_create");
        }

        //查数据库
        baseMapper.selectPage(pageCourse, wrapper);

        //数据
        List<EduCourse> records = pageCourse.getRecords();//用来存放查询出来的数据
        long current = pageCourse.getCurrent(); //当前页
        long pages = pageCourse.getPages();//总页数
        long size = pageCourse.getSize();//每页显示条数
        long total = pageCourse.getTotal();//总计录条数
        boolean hasNext = pageCourse.hasNext();//是否有下一页
        boolean hasPrevious = pageCourse.hasPrevious();//是否有上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //根据课程id，编写SQL查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
